package edu.towson.cosc.cosc455.pjemmott;

import java.util.*;
import java.io.*;

public class SemanticAnalyzer {
static PrintStream out;
	//Arrays used for converting to html tags
	String[] html = { "<html>", "</html>", "<head>", "</head>", "<title>",
			"</title>", "<p>", "</p>", "<b>", "</b>", "<i>", "</i>",
			"<iframe src=\"", "\"/>>", "<audio controls> <source src=\"",
			"\"></audio>", "<li>", "</li>", "", "", "<ul>", "</ul>", "", "",
			"", "\n" };
	String[] array = { "#START DOCUMENT", "#END DOCUMENT", "#START HEAD",
			"#END HEAD", "#START TITLE", "#END TITLE", "#START PARAGRAPH",
			"#END PARAGRAPH", "#START BOLD", "#END BOLD", "#START ITALICS",
			"#END ITALICS", "#START VIDEO", "#END VIDEO", "#START AUDIO",
			"#END AUDIO", "#START ITEM", "#END ITEM", "#START USE", "#END USE",
			"#START LIST", "#END LIST", "#START DEFINE", "#END DEFINE",
			"#NAME", "#NEWLINE" };
	//stack for printing final values
	Stack s2 = new Stack();

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
	}

	// converts the tags to html tags
	public void convert(Stack s, String[] variable, String[] values)
			throws CompilerException {
		String token = s.pop().toString();

		// used to prevent empty stack exception
		while (!s.peek().toString().equalsIgnoreCase("#START DOCUMENT")) {
			// this block used to get rid of these tags as there are no html
			// equivalents
			if (token.equalsIgnoreCase("#START DEFINE")) {
				token = s.pop().toString();

			} else if (token.equalsIgnoreCase("#NAME")) {
				token = s.pop().toString();

			} else if (token.equalsIgnoreCase("#VALUE")) {
				token = s.pop().toString();

				token = s.pop().toString();

			}

			else if (token.equalsIgnoreCase("#END DEFINE")) {
				token = s.pop().toString();

			}
			//end block
			
			//in the case of text which could be a variable or other
			if (token.charAt(0) != '#') {
				//checks to see if text is a variable
				for (int j = 0; j < variable.length; j++) {

					if (token.equalsIgnoreCase(variable[j])) {
						s2.push(values[j]);
						token = s.pop().toString();

						break;
					} else {
						//text is just text
						s2.push(token);
						token = s.pop().toString();

						break;
					}

				}

			}
			//replaces tags with html tags here
			for (int i = 0; i < html.length; i++) {

				if (token.equalsIgnoreCase(array[i])) {
					s2.push(html[i]);
					token = s.pop().toString();
				}

			}

		}
		//pushes <html> to top of second stack for printing
		s2.push("<html>");
		print();
	}

	// prints value of stack to output.txt within the workspace
	public void print() throws CompilerException {
		try {
			out = new PrintStream(new FileOutputStream("output.txt"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		// prints to output.txt until empty
		while (s2.isEmpty() != true) {
			System.out.println(s2.peek());

			out.print(s2.pop().toString());
			out.println("");
		}
		out.close();
	}

}
